package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.AssessDao;
import com.example.child_protection.dto.info.AssessDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.AssessRequestDTO;
import com.example.child_protection.dto.response.AssessListResponseDTO;
import com.example.child_protection.dto.response.AssessResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.AssessModel;
import com.example.child_protection.service.AssessQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 高危查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class AssessQueryServiceImpl extends MyBaseService implements AssessQueryService {

    @Autowired
    public AssessDao assessDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public AssessResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.ASSESS_FIND_ID, requestDTO, AssessResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, AssessResponseDTO>() {
            @Override
            protected AssessResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                AssessResponseDTO responseDTO = new AssessResponseDTO();
                AssessModel assessModel = assessDao.findId(requestDTO.getId());
                AssessDTO assess = new AssessDTO();
                BeanUtils.copyProperties(assessModel, assess);
                responseDTO.setInfo(assess);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public AssessListResponseDTO findList(AssessRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.ASSESS_FIND_LIST, requestDTO, AssessListResponseDTO.class, new MyServiceExecutor<AssessRequestDTO, AssessListResponseDTO>() {
            @Override
            protected AssessListResponseDTO myprocess(AssessRequestDTO requestDTO) throws Exception {
                AssessListResponseDTO responseDTO = new AssessListResponseDTO();
                AssessModel req = new AssessModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<AssessModel> lists = assessDao.findPage(req);
                List<AssessDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), AssessDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
